package com.yzw.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzw.common.domain.EduChapter;
import com.yzw.common.domain.EduCourse;
import com.yzw.common.domain.EduCourseDescription;
import com.yzw.common.domain.EduVideo;
import com.yzw.common.exception.CustomException;
import com.yzw.common.mapper.EduCourseMapper;
import com.yzw.common.vo.CourseVo;
import com.yzw.common.vo.EduCourseVo;
import com.yzw.common.vo.EduPublishVo;
import com.yzw.course.service.EduChapterService;
import com.yzw.course.service.EduCourseDescriptionService;
import com.yzw.course.service.EduCourseService;
import com.yzw.course.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 *
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse>
        implements EduCourseService {

    @Resource
    private EduCourseDescriptionService eduCourseDescriptionService;
    @Resource
    private EduVideoService eduVideoService;
    @Resource
    private EduChapterService eduChapterService;

    @Transactional
    @Override
    public void delete(Long id) {
        LambdaUpdateWrapper<EduCourse> updateWrapper = new UpdateWrapper<EduCourse>().lambda();
        updateWrapper.set(EduCourse::getIsDeleted, 1).eq(EduCourse::getId, id);
        LambdaQueryWrapper<EduCourseDescription> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduCourseDescription::getId, id);
        eduCourseDescriptionService.remove(queryWrapper);
        LambdaQueryWrapper<EduVideo> queryWrapperVideo = new LambdaQueryWrapper<>();
        queryWrapperVideo.eq(EduVideo::getCourseId, id);
        eduVideoService.remove(queryWrapperVideo);
        LambdaQueryWrapper<EduChapter> queryWrapperChapter = new LambdaQueryWrapper<>();
        queryWrapperChapter.eq(EduChapter::getCourseId, id);
        eduChapterService.remove(queryWrapperChapter);
        baseMapper.update(new EduCourse(), updateWrapper);
    }

    @Override
    public Page<EduCourse> selCourse(Integer pageNum, Integer pageSize, CourseVo courseVo) {
        Page<EduCourse> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<EduCourse> queryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(courseVo.getTitle())) {
            queryWrapper.like(EduCourse::getTitle, courseVo.getTitle());
        }
        if (!ObjectUtils.isEmpty(courseVo.getStatus())) {
            queryWrapper.eq(EduCourse::getStatus, courseVo.getStatus());
        }
        queryWrapper.eq(EduCourse::getIsDeleted, 0);
        Page<EduCourse> p = baseMapper.selectPage(page,queryWrapper);
        return p;
    }

    @Transactional
    @Override
    public String addCourse(EduCourseVo eduCourseVo) {
        // 添加课程基本信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(eduCourseVo,eduCourse);
        eduCourse.setIsDeleted(new Byte("0"));
        int insert = baseMapper.insert(eduCourse);
        if (insert <= 0){
            throw new CustomException(20002,"添加课程基本信息失败");
        }

        // 获取添加课程后的id
        String cid = eduCourse.getId();

        // 向课程简介表添加课程简介
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(eduCourseVo.getDescription());
        eduCourseDescription.setId(cid);
        eduCourseDescriptionService.save(eduCourseDescription);
        return cid;
    }

    /**
        根据id查询课程id
     */
    @Transactional
    @Override
    public EduCourseVo selCourseById(String courseId) {
        EduCourse eduCourse = baseMapper.selectById(courseId);
        EduCourseVo eduCourseVo = new EduCourseVo();
        BeanUtils.copyProperties(eduCourse,eduCourseVo);

        EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getById(courseId);
        if (eduCourseDescription != null) {
            eduCourseVo.setDescription(eduCourseDescription.getDescription());
        }
        return eduCourseVo;
    }

    @Override
    public void updCourse(EduCourseVo eduCourseVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(eduCourseVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if (update <= 0){
            throw new CustomException(20001,"修改课程信息失败");
        }
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(eduCourse.getId());
        eduCourseDescription.setDescription(eduCourseVo.getDescription());
        eduCourseDescriptionService.updateById(eduCourseDescription);
    }

    @Override
    public EduPublishVo getPublish(String courseId) {
        EduPublishVo eduPublishVo = baseMapper.getPublish(courseId);
        return eduPublishVo;
    }
}
